﻿using System;
using System.Globalization;
using System.Windows.Forms;
using Campus.Main.Basic.Settings;
using cpmntctrl;
using dbcntn;

namespace Campus.Main.Basic
{
    public partial class Devamsizlik : DefaultForm
    {
        public Devamsizlik()
        {
            InitializeComponent();
            aToDb=new ConnectionToDb(Helper.ConnectionString);
            StudentId = null;
        }

        public Devamsizlik(string student_id)
        {
            InitializeComponent();
            aToDb = new ConnectionToDb(Helper.ConnectionString);
            StudentId = student_id;
        }

        public override void Initialize()
        {
            clearfield();
            Quer = Query.KesinKayit + " and a.pol_id=" + Pol;
            aToDb.AddItemDBGridView(dataGridView1,Quer);
            if (dataGridView1.RowCount != 0)
            {
                StudentId = Convert.ToString(dataGridView1[0, dataGridView1.SelectedRows[0].Index].Value);
                string quer2 = Query.Izin + " where student_id like('" + StudentId + "')";
                aToDb.AddItemDBGridView(dataGridView3, quer2);
                ControlComponent.UnVisibleCells(dataGridView3, 0);
                string quer1 = Query.Izinsiz + " where student_id like('" + StudentId + "')";
                aToDb.AddItemDBGridView(dataGridView2, quer1);
                ControlComponent.UnVisibleCells(dataGridView2, 0);
                toolStripLabel1.Text = "Gelmedigi gunlerin zayisi:" +
                                       aToDb.SelectField("select count(*) from izinsiz where student_id like('" +
                                                         StudentId + "')");
                toolStripLabel2.Text = "Izinlerin toplam sayisi:" +
                                       aToDb.SelectField("select count(*) from izin where student_id like('" + StudentId +
                                                         "')") +
                                       " .Izinlerin toplam gunleri:" +
                                       aToDb.SelectField("select sum(day) from izin where student_id like('" + StudentId +
                                                         "')");
            }
        }

        public int Pol { get; set; }
        private string   Quer{ get; set; }
        public string StudentId { get; set; }

        void clearfield()
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox5.Clear();
            textBox6.Clear();
            textBox7.Clear();
            dateTimePicker1.Value = dateTimePicker2.Value = dateTimePicker3.Value = DateTime.Now;
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            checkBox1.Checked=checkBox3.Checked=checkBox2.Checked = false;
        }

        private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
        {
           textBox1.Enabled=checkBox1.Checked;

        }
        private void checkBox2_CheckedChanged(object sender, System.EventArgs e)
        {
            textBox2.Enabled =checkBox2.Checked ;
        }

        private void Devamsizlik_Load(object sender, System.EventArgs e)
        {
            Initialize();
        }

        private void dataGridView1_CellClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
        {
            if (e.RowIndex==-1||e.ColumnIndex==-1)
                return;
            if (dataGridView1[0, e.RowIndex].Value != null)
            {
                StudentId = Convert.ToString(dataGridView1[0, e.RowIndex].Value);
                string quer1 = Query.Izinsiz + " where student_id like('" + StudentId + "')";
                aToDb.AddItemDBGridView(dataGridView2, quer1);
                ControlComponent.UnVisibleCells(dataGridView2, 0);
                string quer2 = Query.Izin + " where student_id like('" + StudentId + "')";
                aToDb.AddItemDBGridView(dataGridView3, quer2);
                ControlComponent.UnVisibleCells(dataGridView3, 0);
                toolStripLabel1.Text = "Gelmedigi gunlerin zayisi:" +
                                       aToDb.SelectField("select count(*) from izinsiz where student_id like('" +
                                                         StudentId + "')");
                toolStripLabel2.Text = "Izinlerin toplam sayisi:" +
                                       aToDb.SelectField("select count(*) from izin where student_id like('" + StudentId +
                                                         "')") +
                                       " .Izinlerin toplam gunleri:" +
                                       aToDb.SelectField("select sum(day) from izin where student_id like('" + StudentId +
                                                         "')");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (StudentId!=null)
            {
                string date_no = dateTimePicker1.Value.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
                string test=aToDb.SelectField("select ifnull(id,0) from izin where student_id like('"+StudentId+"') and '"+date_no+"' between exit_date and enter_date");
                if (test!="0")
                {
                    MessageBox.Show("OGRENCI BU TARIHTE IZINLI DEVAMSIZLIK GIRILEMEZ!!!", "Uyari");
                    return;
                }
                string[] par = {"student_id","date_no", "description"};
                string[] val = {StudentId,date_no,textBox7.Text};
                aToDb.InsertIntoTable("izinsiz",par,val);
                Initialize();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Ceza f=new Ceza(StudentId);
            f.FormClosed += Ceza_close;
            f.ShowDialog();
        }

        private void Ceza_close(object sender, FormClosedEventArgs e)
        {
            Close();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            //if (textBox4.Text.Length>0&&textBox5.Text.Length>0&&textBox6.Text.Length>0)
            {
               // long day = dateTimePicker2.Value - dateTimePicker3.Value;
                string enter_date = dateTimePicker3.Value.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
                string exit_date = dateTimePicker2.Value.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
                int day  =(int)(dateTimePicker3.Value.Date - dateTimePicker2.Value.Date).TotalDays;
                string telefon = textBox5.Text.Trim('/', ',', '.');
                string adres = textBox4.Text;
                string dedscription = textBox6.Text;
                string[] param = {"student_id", "exit_date", "enter_date", "day", "telefon", "adres", "description"};
                aToDb.InsertIntoTable("izin",param,StudentId,exit_date,enter_date,Convert.ToString(day),telefon,adres,dedscription);
                Initialize();
            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            textBox3.Enabled = checkBox3.Checked;
        }

        private void textBox_TextChanged(object sender, EventArgs e)
        {
            string query = Query.KesinKayit;
            if (checkBox1.Checked)
            {
                query += " and a.student_id like('" + textBox1.Text + "%')";
            }
            if (checkBox2.Checked)
            {
                query += " and a.name like('" + textBox2.Text + "%')";
            }
            if (checkBox3.Checked)
            {
                query += " and e.description like('" + textBox3.Text + "%')";
            }
            aToDb.AddItemDBGridView(dataGridView1,query);
        }

        private void dataGridView1_DataSourceChanged(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count==0)
            {
                dataGridView2.DataSource=dataGridView3.DataSource = null;
                return;
            }

            StudentId = Convert.ToString(dataGridView1[0, dataGridView1.SelectedRows[0].Index].Value);
            string quer1 = Query.Izinsiz + " where student_id like('" + StudentId + "')";
            aToDb.AddItemDBGridView(dataGridView2, quer1);
            ControlComponent.UnVisibleCells(dataGridView2, 0);
            string quer2 = Query.Izin + " where student_id like('" + StudentId + "')";
            aToDb.AddItemDBGridView(dataGridView3, quer2);
            ControlComponent.UnVisibleCells(dataGridView3, 0);
            toolStripLabel1.Text = "Gelmedigi gunlerin zayisi:" +
                aToDb.SelectField("select count(*) from izinsiz where student_id like('" + StudentId + "')");
            toolStripLabel2.Text = "Izinlerin toplam sayisi:" +
                                   aToDb.SelectField("select count(*) from izin where student_id like('" + StudentId +
                                                     "')") +
                                   " .Izinlerin toplam gunleri:" +
                                   aToDb.SelectField("select sum(day) from izin where student_id like('" + StudentId +
                                                     "')");
           
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Silmek istiyormusunuz?", "Dikkat", MessageBoxButtons.YesNo, MessageBoxIcon.Stop) == DialogResult.Yes)
            {
                string id =
                    Convert.ToString(dataGridView2.SelectedRows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value);
                aToDb.DeleteTable("izinsiz", "id", id);
                StudentId = Convert.ToString(dataGridView1[0, dataGridView1.SelectedRows[0].Index].Value);
                string quer1 = Query.Izinsiz + " where student_id like('" + StudentId + "')";
                aToDb.AddItemDBGridView(dataGridView2, quer1);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Silmek istiyormusunuz?", "Dikkat", MessageBoxButtons.YesNo, MessageBoxIcon.Stop) == DialogResult.Yes)
            {
                string id =
                    Convert.ToString(dataGridView3.SelectedRows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value);
                aToDb.DeleteTable("izin", "id", id);
                StudentId = Convert.ToString(dataGridView1[0, dataGridView1.SelectedRows[0].Index].Value);
                string quer1 = Query.Izin + " where student_id like('" + StudentId + "')";
                aToDb.AddItemDBGridView(dataGridView3, quer1);
            }
        }
    }
}
